package com.example.mvt;


import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends Activity {
	
	String username;
	 public static final int NOTIFICATION_ID = 1;
	private NotificationManager mNotificationManager;
	NotificationCompat.Builder builder;
	Context context = this;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd");
		Calendar cal = Calendar.getInstance();
		String date = dateFormat.format(cal.getTime());
	
		if(date.equals("01")){
			sendNotification("Vote MVT for this month!");
		}
		if(date.equals("25")){
			sendNotification("Remember to Vote MVT for this month!");
		}
		
		ImageButton ib = (ImageButton)findViewById(R.id.osh);
	    ib.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(MainActivity.this, Oshkosh.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib2 = (ImageButton)findViewById(R.id.lock);
	    ib2.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(MainActivity.this, Lockport.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib3 = (ImageButton)findViewById(R.id.sing); 
	    ib3.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(MainActivity.this, Singapore.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib4 = (ImageButton)findViewById(R.id.vote);
	    ib4.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(MainActivity.this, Vote.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
		ImageButton ib5 = (ImageButton)findViewById(R.id.win1);
	    ib5.setOnClickListener(new View.OnClickListener() {
	        @Override
	        public void onClick(View v) {
	        	Intent myIntent = new Intent(MainActivity.this, Winners.class);
				startActivityForResult(myIntent, 0);
	        }
	    });
	}
	
	private void sendNotification(String msg)
	{
		mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
		new Intent(context, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT), 0);

			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
			.setSmallIcon(R.drawable.ic_launcher)
			.setContentTitle("MVT")
			.setStyle(new NotificationCompat.BigTextStyle()
			.bigText(msg))
			.setAutoCancel(true)
			.setContentText(msg)
			.setLights(0xff00ff00, 300, 1000)
			.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);

			mBuilder.setContentIntent(contentIntent);
			mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
			mBuilder=new NotificationCompat.Builder(context)
		.setSmallIcon(R.drawable.ic_launcher)
		.setContentTitle("MVT")
		.setStyle(new NotificationCompat.BigTextStyle()
		.bigText(msg))
		.setAutoCancel(true)
		.setContentText(msg)
		.setLights(0xff00ff00, 300, 1000)
		.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);

		mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		
	    // Handle item selection
	    switch (item.getItemId()) {	
	        case R.id.action_logout:
	        	new Logout().execute();
	        	Intent myIntent3 = new Intent(MainActivity.this, LoginActivity.class);
				startActivityForResult(myIntent3, 0);
				MainActivity.this.finish();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		
		new getLoginInfo().execute();
		
	}
	
	//Get Login Info
		private class Logout extends AsyncTask<Void, Void, String>{
			@Override
			protected String doInBackground(Void... nothing) {
				
				MVTDataSource mds2 = new MVTDataSource(MainActivity.this);
				
				mds2.open();
				
				try {
					mds2.logoutUser();
				} catch (Exception e) {
					e.printStackTrace();
				}
				
				mds2.close();
				
				return "";
			}
			
			@Override
			protected void onPostExecute(String whatever) {
					Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
					startActivityForResult(myIntent, 0);
					MainActivity.this.finish();
			}
			
		}
	
	
	//Get Login Info
	private class getLoginInfo extends AsyncTask<Void, Void, String>{
		@Override
		protected String doInBackground(Void... nothing) {
			
			MVTDataSource mds2 = new MVTDataSource(MainActivity.this);
			
			mds2.open();
			
			try {
				username = mds2.getUsername();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			mds2.close();
			
			return username;
		}
		
		@Override
		protected void onPostExecute(String whatever) {
			if (username.equals("user")){
				Intent myIntent = new Intent(MainActivity.this, LoginActivity.class);
				startActivityForResult(myIntent, 0);
				return;
			}
			else{
				return;
			}
		}
		
	}

}
